# -*- coding: utf-8 -*-

# @File  : record_util.py
# @Author: Lomo
# @Site  : lomo.space
# @Date  : 2020-02-10
# @Desc  : Record Model

import records

from config.db import DB_CONFIG


class RecordUtil:
    def __init__(self, _type="LOCAL"):
        self.host = DB_CONFIG.get(_type).get('host')
        self.port = DB_CONFIG.get(_type).get('port')
        self.user = DB_CONFIG.get(_type).get('user')
        self.password = DB_CONFIG.get(_type).get('password')
        self.database = DB_CONFIG.get(_type).get('database')

        conn_str = "mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format(self.user, self.password,
                                                                self.host, self.port,
                                                                self.database)
        self.db = records.Database(conn_str)

    def connect(self):
        return self.db

    def exec(self, sql):
        return self.db.query(sql)

    def close(self):
        return self.db.close()
